package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;

public class DoctorSettingClientAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<CustomerInfoEntity> entities;
	onClickFriendAttentionListener attentionListener;
	onClickFriendHeadListener clickFriendHeadListener;
	private final ImageLoader mImageLoader;
	private final Drawable maleDrawable,femalDrawable,dayTicketDrawable,monthTicketDrawable;
	private Boolean isSalonMemberList =false;
	private View clickView;
	private boolean isMessageVisible = false;
	private ConcurrentHashMap<String,List<MessageEntity>> messageMap;
	private Drawable unKownDrawable;
	public DoctorSettingClientAdapter(Context context,List<CustomerInfoEntity> entities) {
		inflater = LayoutInflater.from(context);
		this.entities = entities;
		mImageLoader = ImageLoader.getInstance();
	
		femalDrawable = context.getResources().getDrawable(R.drawable.women);
		maleDrawable = context.getResources().getDrawable(R.drawable.man);
		unKownDrawable = context.getResources().getDrawable(R.drawable.sex_unknown);
		dayTicketDrawable = context.getResources().getDrawable(R.drawable.day_ticket);
		monthTicketDrawable = context.getResources().getDrawable(R.drawable.month_ticket);
	}

	@Override
	public int getCount() {
		return entities.size();
	}

	@Override
	public Object getItem(int position) {
		return entities.get(position);
	}

	@Override 
	public long getItemId(int position) {
		return position;
	}
	
	public void setIsSalonMemberList(Boolean b){
		this.isSalonMemberList =b;
	}

	
	public void setAdapterData(ArrayList<CustomerInfoEntity> entitie){
		entities.clear();
		entities.addAll(entitie);
		notifyDataSetChanged();
	}
	
	public View getClickView(){
		return clickView;
	}
	
	public void setClickView(View v){
		this.clickView = v;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final viewHolder holder;
		ConcurrentHashMap<String, List<MessageEntity>> messageMap = HTalkApplication.getAppData().messageCllection;
		final CustomerInfoEntity customerInfoEntity = entities.get(position);
		if (convertView ==null||convertView.getTag()==null) {
			holder = new viewHolder();
			convertView = inflater.inflate(R.layout.doctor_setting_client_item,null) ;
			holder.head = (ImageView) convertView.findViewById(R.id.head_image);
			holder.personName = (TextView) convertView.findViewById(R.id.name);
			holder.headSex = (ImageView) convertView.findViewById(R.id.head_sex);
			holder.v = (ImageView) convertView.findViewById(R.id.v);
			holder.note = (TextView) convertView.findViewById(R.id.note);
			holder.attentionSalon = (TextView)convertView.findViewById(R.id.is_friend);
			holder.service_time = (TextView)convertView.findViewById(R.id.doctor_service_time);
			holder.mesgCount = (TextView)convertView.findViewById(R.id.item_dele);
			holder.mesgButton = (Button)convertView.findViewById(R.id.is_friend_button);
			convertView.setTag(holder);
		}else {
			holder = (viewHolder) convertView.getTag();
		}
		messageMap = HTalkApplication.getAppData().messageCllection;
		mImageLoader.displayImage(customerInfoEntity.getSex(), customerInfoEntity.getNormalHeadIcon(), holder.head);
//		在线人数
//		holder.onlineNum.setText("66人");
		//姓名
		holder.personName.setText(customerInfoEntity.getName());
		
		//是否是医师沙龙
		if (customerInfoEntity.isShow() == true || customerInfoEntity.isAudit() == true) {
			holder.v.setVisibility(View.VISIBLE);
		}else {
			holder.v.setVisibility(View.GONE);
		}
		
		if (messageMap.containsKey(customerInfoEntity.getId()) && messageMap.get(customerInfoEntity.getId()).size() != 0) {
			holder.mesgCount.setText(messageMap.get(customerInfoEntity.getId()).size()+"");
			holder.mesgCount.setVisibility(View.VISIBLE);
		}else {
			holder.mesgCount.setVisibility(View.GONE);
		}
		
		//签名
		holder.note.setText(customerInfoEntity.getDescription());
		//消息
	/*	if (isMessageVisible) {
			holder.message.setVisibility(View.VISIBLE);
			if (messageMap.contains(customerInfoEntity.getId()) && messageMap.get(customerInfoEntity.getId()).size() != 0) {
				holder.message.setText(messageMap.get(customerInfoEntity.getId()).size());
			}else {
				holder.message.setVisibility(View.GONE);
			}
		}else {
			holder.message.setVisibility(View.GONE);
		}*/
		
		if("60".equals(customerInfoEntity.getServiceStatusCode())){
			holder.mesgButton.setText("处理");
			holder.service_time.setText(customerInfoEntity.getServiceStatus());	
			holder.mesgButton.setVisibility(View.VISIBLE);
			holder.attentionSalon.setVisibility(View.GONE);
		}else if("70".equals(customerInfoEntity.getServiceStatusCode())){
			holder.attentionSalon.setVisibility(View.GONE);
			holder.mesgButton.setText("已处理");
			holder.mesgButton.setVisibility(View.VISIBLE);
			holder.service_time.setText(customerInfoEntity.getServiceStatus());	
		}else{
			holder.attentionSalon.setText(customerInfoEntity.getServiceStatus());
			holder.service_time.setText(customerInfoEntity.getServiceTime());
			holder.mesgButton.setVisibility(View.GONE);
			holder.attentionSalon.setVisibility(View.VISIBLE);
		}
		
		
		//点击头像进入个人资料
		holder.head.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (clickFriendHeadListener != null) {
					clickFriendHeadListener.onClickFriendHead(customerInfoEntity , position);
				}
			}
		});
		//点击查看客户退号申请
		holder.mesgButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (attentionListener != null) {
					attentionListener.onClickFriendAttention(entities.get(position) , position );
				}
					setClickView(v);
			}
		});
		
		return convertView;
	}
	
	private static  class viewHolder {
		private TextView note;//描述
		private ImageView head; // 头像
		private TextView service_time;//服务时间
		private TextView personName; // 名称
		private ImageView headSex; // 性别
		private ImageView v;
		private TextView mesgCount;
//		private TextView message;//消息的图标
		private TextView attentionSalon;//服务状态
		private Button mesgButton;
	}
	
	public interface onClickFriendHeadListener{
		void onClickFriendHead(CustomerInfoEntity entity , int positon);
	}
	
	public void setonClickFriendHeadListener(onClickFriendHeadListener headListener){
		this.clickFriendHeadListener = headListener;
	};
	
	public interface onClickFriendAttentionListener{
		void onClickFriendAttention(CustomerInfoEntity entity , int positon);
	}
	
	public void setonClickFriendAttentionListener(onClickFriendAttentionListener attentionListener){
		this.attentionListener = attentionListener;
	};
	
	public void setMessageVisible(Boolean isMessageVisible){
		this.isMessageVisible  = isMessageVisible;
	}

}
